class Node {
    constructor(value) {
        this.value = value;
        this.next = null;
    }
}
class LinkedList {
    constructor() {
        this.head = null;
        this.tail = null;
        this.length = 0;
    }
    //? 后插
    append(value) {
        const node = new Node(value);
        if (!this.head) {
            this.head = node;
            this.tail = node;
        } else {
            this.tail.next = node;
            this.tail = node;
        }
        this.length++;
    }
    //? 前插
    prepend(value) {
        const node = new Node(value);
        if (!this.head) {
            this.head = node;
            this.tail = node;
        } else {
            node.next = this.head;
            this.head = node;
        }
        this.length++;
    }
    //? 指定位置插入
    insert(index, value) {
        if (index < 0 || index > this.length) {
            console.error("Index out of bounds");
            return;
        }
        if (index === 0) {
            this.prepend(value);
            return;
        }
        if (index === this.length) {
            this.append(value);
            return;
        }
        const node = new Node(value);
        let prevNode = this.head;
        for (let i = 0; i < index - 1; i++) {
            prevNode = prevNode.next;
        }
        node.next = prevNode.next;
        prevNode.next = node;
        this.length++;
    }
    //? 移除节点
    remove(index) {
        if (index < 0 || index >= this.length) {
            console.error("Index out of bounds");
            return;
        }
        if (index === 0) {
            this.head = this.head.next;
            this.length--;
            return;
        }
        let prevNode = this.head;
        let currentNode = this.head.next;
        for (let i = 1; i < index; i++) {
            prevNode = currentNode;
            currentNode = currentNode.next;
        }
        prevNode.next = currentNode.next;
        if (index === this.length - 1) {
            this.tail = prevNode;
        }
        this.length--;
    }
    //? 翻转链表
    reverse() {
        if (!this.head) {
            console.error("LinkedList is empty");
            return;
        }
        let prevNode = null;
        let currentNode = this.head;
        let nextNode = currentNode.next;
        while (currentNode) {
            currentNode.next = prevNode;
            prevNode = currentNode;
            currentNode = nextNode;
            nextNode = currentNode ? currentNode.next : null;
        }
        this.tail = this.head;
        this.head = prevNode;
    }
    //? 打印链表元素
    print() {
        let node = this.head;
        const values = [];
        while (node) {
            values.push(node.value);
            node = node.next;
        }
        console.log(values.join(" -> "));
    }
}
// Usage Example:
const list = new LinkedList();
list.append(10);
list.append(20);
list.append(30);
list.prepend(5);
list.insert(2, 15);
list.print(); // Output : 5 ->10 ->15 ->20 ->30
list.remove(4);
list.print(); // Output : 5 ->10 ->15 ->20
list.reverse();
list.print(); // Output : 20 ->15 ->10 ->5
